package com.blvtphan.tipocketclubs.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.blvtphan.tipocketclubs.R;
import com.blvtphan.tipocketclubs.custom.CustomTextView;
import com.blvtphan.tipocketclubs.model.PlaceDetailModel;

public class NearbyItemAdapter extends BaseAdapter {

	private Context mContext;
	private List<PlaceDetailModel> lstPlaceNearby;

	public NearbyItemAdapter(Context context, List<PlaceDetailModel> lstPlaceNearby) {
		this.mContext = context;
		this.lstPlaceNearby = lstPlaceNearby;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View rowView = inflater.inflate(R.layout.item_nearby_place_layout, parent, false);
		
		CustomTextView tvPlaceName = (CustomTextView) rowView.findViewById(R.id.txt_nearby_place_name);
		CustomTextView tvPlaceType = (CustomTextView) rowView.findViewById(R.id.txt_nearby_place_type);
		CustomTextView tvPlaceDistance = (CustomTextView) rowView.findViewById(R.id.txt_nearby_place_distance);
		CustomTextView tvPlaceAddress = (CustomTextView) rowView.findViewById(R.id.txt_nearby_place_address);

		tvPlaceName.setText(lstPlaceNearby.get(position).getName());
		tvPlaceType.setText(lstPlaceNearby.get(position).getCategory());
		tvPlaceDistance.setText(convertToDisplayDistance(Float.valueOf(lstPlaceNearby.get(position).getDistance())));
		tvPlaceAddress.setText(lstPlaceNearby.get(position).getAddress());
		
		return rowView;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return lstPlaceNearby.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return lstPlaceNearby.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}
	
	private String convertToDisplayDistance(float value) {
		String result = "";
		float kmValue = value/1000;
		DecimalFormat df = new DecimalFormat();
		df.setMaximumFractionDigits(1);
		result = String.valueOf(df.format(kmValue)) + " km";
		return result;
	}

}
